<template>
  <div class="login">
    <div class="pannel">
      <div class="title">系统登录</div>
      <el-form :model="formData" :rules="rules" ref="form">
        <el-form-item prop="account">
          <el-input
            class="mt20"
            type="text"
            placeholder="请输入内容"
            prefix-icon="iconfont icon-user"
            v-model="formData.account"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="status ? 'password' : 'text'"
            class="mt20"
            placeholder="请输入内容"
            v-model="formData.password"
          >
            <i slot="prefix" class="iconfont icon-password"></i>
            <!-- 眼睛的两个图标 -->
            <i
              slot="suffix"
              @click="status = !status"
              class="iconfont"
              :class="status ? 'icon-close' : 'icon-open'"
            ></i>
          </el-input>
        </el-form-item>

        <el-button type="primary" class="btn mt20" @click="loginBtn()">
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/users";
export default {
  data() {
    let checkAccount = (rule, value, callback) => {
      //非空验证
      if (!value) {
        return callback(new Error("请输入账号"));
      }

      //正则验证
      // let reg = /^1[3-9][0-9]{9}$/;
      // if (!reg.test(value)) {
      //   return callback(new Error("手机号格式不对"));
      // }
      //一致性：【扩展】
      // let { account, password } = this.formData;
      // if (account != password) {
      //   return callback(new Error("两次密码不一致"));
      // }
      //上面的if都不成立  通过验证
      return callback();
    };
    return {
      formData: {
        account: "",
        password: "",
      },
      status: true, //true:闭眼 password
      //定义表单验证规则--element自带验证
      rules: {
        account: [{ validator: checkAccount, trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    loginBtn() {
      this.$refs.form.validate(async (status) => {
        if (status) {
          //返回数据
          let { id, role, token, code,msg} = await login(this.formData);
          if (code == 0) {
            //将数据 存储到 本地存储中
            localStorage.setItem("user", JSON.stringify({ id, role, token }));
            //提示并跳转
            this.$message({
              type: "success",
              message: "登录成功",
              duration: 1000,
            });
            //延时器 setTimeout
            setTimeout(() => {
              this.$router.push("/home");
            }, 1000);
          }else{
              this.$message.error(msg)
          }
        }
      });
    },
  },
  created() {},
};
</script>

<style lang="less" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #2d3a4b;
}
.pannel {
  width: 400px;
  height: 300px;
}
.title {
  /**
    * 先写位置信息 定位 position margin float display：flex
    * 盒子本身宽高 
    * 文本相关信息 fonts fontweight 
    * css3特效 border-radius  transi 
    ***/
  width: 100%;
  height: 50px;
  line-height: 50px;
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
}
.btn {
  width: 100%;
}
// 覆盖element 自带的样式失败
// 原因：作用域导致的
// 穿透css的作用域 深度修改样式 类名的 /deep/
/deep/.el-input__inner {
  background-color: rgba(0, 0, 0, 0);
  color: #fff;
}
/deep/.el-input__prefix i {
  height: 40px !important;
  line-height: 40px !important;
}
/deep/.el-input__suffix i {
  height: 40px !important;
  line-height: 40px !important;
}
</style>